package com.design.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.reggie.dto.DishDto;
import com.design.reggie.entity.Dish;
import com.design.reggie.entity.DishFlavor;
import com.design.reggie.service.DishFlavorService;
import com.design.reggie.service.DishService;
import com.design.reggie.mapper.DishMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JPY
 * @description 针对表【dish(菜品管理)】的数据库操作Service实现
 * @createDate 2023-01-30 16:02:45
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
        implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    /**
     * 新增菜品，同时保存对应的口味数据
     * 由于同时操作两张表，所以需要开启事务
     */
    @Transactional
    public void addWithFlavors(DishDto dishDto) {
        // 保存菜品
        this.save(dishDto);
        // 获取菜品的id，用于保存口味时候用
        Long dishId = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }

        // 保存菜品口味到菜品口味表 dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询菜品和口味数据
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavors(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        // 拷贝属性
        BeanUtils.copyProperties(dish, dishDto);
        // 构造查询条件
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        // 口味表的dishId==菜品id
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());

        // 查询所有符合条件的，用list方法
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 修改菜品信息，同时修改口味
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavors(DishDto dishDto) {
        Long dishId = dishDto.getId();

        // 更新dish表的基本信息
        this.updateById(dishDto);


        // 清除dish_flavor表的相关菜品信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        LambdaQueryWrapper<DishFlavor> queryWrapperDel = new LambdaQueryWrapper<>();
        queryWrapperDel.eq(flavors != null, DishFlavor::getDishId, dishId);
        dishFlavorService.remove(queryWrapperDel);


        // 新增dish_flavor表的相关菜品信息
        List<DishFlavor> flavorList = dishDto.getFlavors();
        for (DishFlavor flavor : flavorList) {
            flavor.setDishId(dishId);
        }
        // 保存菜品口味到菜品口味表 dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据ids删除菜品和口味
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavors(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            // 删除菜品表数据
            this.removeById(id);
            // 删除口味表数据
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorService.remove(queryWrapper);
        }
    }


}




